package de.staatsoper.dienstplanbso;

import java.io.IOException;
import java.util.ArrayList;

import de.staatsoper.dienstplanbso.shiftlist.ShiftListAdapter;
import de.staatsoper.dienstplanbso.shiftlist.ShiftListChild;
import de.staatsoper.dienstplanbso.shiftlist.ShiftListGroup;

import android.os.Bundle;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
	
	private class FetchEmailsRunnable implements Runnable{
		
		private AccountManagerFuture<Bundle> accountManagerFuture;
		private ExpandableListView shiftList;
		private Context context;
		
		public FetchEmailsRunnable(AccountManagerFuture<Bundle> accountManagerFuture, ExpandableListView shiftList, Context context){
			this.accountManagerFuture = accountManagerFuture;
			this.shiftList = shiftList;
			this.context = context;
		}
		
		public void run() {
	    	Bundle authTokenBundle;
	    	String authToken = "";
			try {
				authTokenBundle = accountManagerFuture.getResult();
				authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
			}
			catch (OperationCanceledException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (AuthenticatorException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			ShiftListGroup aGroup = new ShiftListGroup("Test Group Name");
			ShiftListChild aChild = new ShiftListChild(authToken);
			
			ArrayList<ShiftListChild> children = new ArrayList<ShiftListChild>();
			children.add(aChild);
			aGroup.setItems(children);
			
			ArrayList<ShiftListGroup> groups = new ArrayList<ShiftListGroup>();
			groups.add(aGroup);
			
			ShiftListAdapter adapter = new ShiftListAdapter(this.context, groups);
			
			this.shiftList.setAdapter(adapter);
			
	    }
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ExpandableListView scheduleList = (ExpandableListView) findViewById(R.id.shift_schedule_list);
		
		AccountManager accountManager = AccountManager.get(this);
		Account[] accounts = accountManager.getAccountsByType("com.google");
				
		//TODO: Let user select an account in preferences and/or dialog...
		if (accounts.length > 0){
			AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(accounts[0], "mail", null, (Activity)this, null, null);
				
			new Thread(new FetchEmailsRunnable (accountManagerFuture, scheduleList, this)).start();
		}		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
